#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const int N = 2e5 + 5;
int T, n, m, K, a[N], c[N], X[4];
char op[18][N];

namespace taskA {
int p[N << 2];
void init(int x = 1, int l = 1, int r = 1 << K, int d = K, int g = 0) {
	if (!d) {
		p[x] = l;
		return;
	}
	int mid = (l + r) >> 1;
	init(x << 1, l, mid, d - 1, g << 1);
	init(x << 1 | 1, mid + 1, r, d - 1, g << 1 | 1);
	int ls = p[x << 1], rs = p[x << 1 | 1];
	if (op[d][g] == '1') p[x] = a[rs] >= d ? rs : ls;
	else p[x] = a[ls] >= d ? ls : rs;
}
void solve() {
	init();
	ll ans = 0;
	for (int i = 1; i <= m; ++i) {
		int x = (1 << K) / c[i];
		ans ^= (ll)p[x] * i;
	}
	cout << ans << '\n';
}
}

namespace taskB {
int val[N << 2];
ll sum[N << 2], res[N];
void init(int x = 1, int l = 1, int r = 1 << K) {
	val[x] = -1;
	sum[x] = (ll)(l + r) * (r - l + 1) / 2;
	if (l == r) return;
	int mid = (l + r) >> 1;
	init(x << 1, l, mid);
	init(x << 1 | 1, mid + 1, r);
}
inline void pushup(int x, int d) {
	int ls = x << 1, rs = x << 1 | 1;
	if (val[ls] >= 0) {
		int y = val[ls] >= d ? ls : rs;
		val[x] = val[y], sum[x] = sum[y];
	} else {
		val[x] = -1;
		sum[x] = sum[ls] + sum[rs];
	}
}
void upd(int k, int x = 1, int l = 1, int r = 1 << K, int d = K) {
	if (l == r) {
		val[x] = a[l];
		sum[x] = l;
		return;
	}
	int mid = (l + r) >> 1;
	if (k <= mid) upd(k, x << 1, l, mid, d - 1);
	else upd(k, x << 1 | 1, mid + 1, r, d - 1);
	pushup(x, d);
}
void solve() {
	init();
	for (int i = 1; i <= n; ++i) {
		upd(i);
		int x = 1 << (K - __lg(2 * i - 1));
		res[i] = sum[x];
	}
	ll ans = 0;
	for (int i = 1; i <= m; ++i)
		ans ^= (ll)res[c[i]] * i;
	cout << ans << '\n';
}
}

namespace brute {
int b[25];
int getans(int l, int r, int d, int g) {
	if (!d) return 1 << l;
	int mid = (l + r) >> 1;
	int lv = getans(l, mid, d - 1, g << 1), rv = getans(mid + 1, r, d - 1, g << 1 | 1);
	int res = 0;
	if (op[d][g] == '0') {
		for (int i = l; i <= mid; ++i) {
			if (lv >> i & 1) {
				if (b[i] < d) res |= rv;
				if (b[i] >= d || b[i] == -1) res |= 1 << i;
			}
		}
	} else {
		for (int i = mid + 1; i <= r; ++i) {
			if (rv >> i & 1) {
				if (b[i] < d) res |= lv;
				if (b[i] >= d || b[i] == -1) res |= 1 << i;
			}
		}
	}
	return res;
}
void solve() {
	ll ans = 0;
	for (int i = 1; i <= m; ++i) {
		K = __lg(2 * c[i] - 1);
		for (int j = 1; j <= c[i]; ++j) b[j] = a[j];
		for (int j = c[i] + 1; j <= (1 << K); ++j) b[j] = -1;
		int t = getans(1, 1 << K, K, 0), res = 0;
		for (int j = 1; j <= (1 << K); ++j)
			if (t >> j & 1) res += j;
		ans ^= (ll)res * i;
	}
	cout << ans << '\n';
}
}

int main() {
	freopen("arena.in", "r", stdin);
	freopen("arena.out", "w", stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin >> n >> m;
	for (int i = 1; i <= n; ++i) cin >> a[i];
	bool chk_c = 1;
	for (int i = 1; i <= m; ++i) cin >> c[i], chk_c &= ((c[i] & (c[i] - 1)) == 0);
	K = __lg(2 * n - 1);
	for (int i = 1; i <= K; ++i)
		for (int j = 0; j < (1 << (K - i)); ++j)
			cin >> op[i][j];
	cin >> T;
	while (T--) {
		cin >> X[0] >> X[1] >> X[2] >> X[3];
		for (int i = 1; i <= n; ++i) a[i] ^= X[i & 3];
		if (n <= 8 && m <= 8) brute::solve();
		else if (chk_c) taskA::solve();
		else taskB::solve();
		for (int i = 1; i <= n; ++i) a[i] ^= X[i & 3];
	}
	return 0;
}
